<?php


/*
开发者:LZY
代码完成时间:2025.5.10
*/


header('Content-Type: application/json; charset=utf-8');

class MaoyanAPI {
    private $baseUrl = 'https://piaofang.maoyan.com/i/api/getBoxList';
    private $defaultQueryParams = [
        'date' => '1',
        'isSplit' => 'true',
        'allowAd' => '1',
        'User-Agent' => 'TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzEzNi4wLjAuMCBTYWZhcmkvNTM3LjM2IEVkZy8xMzYuMC4wLjA=',
        'index' => '423',
        'channelId' => '40009',
        'sVersion' => '2',
        'signKey' => 'f8a2f5e8128a5a65e292dbd7d76c7ae6',
        'WuKongReady' => 'h5'
    ];

   
    public function __construct() {
        
    }

    /**
     * 从猫眼API获取原始票房数据。
     *
     */
    private function fetchData() {
        $queryParams = $this->defaultQueryParams;
        $queryParams['timeStamp'] = round(microtime(true) * 1000);
        $url = $this->baseUrl . '?' . http_build_query($queryParams);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, $queryParams['User-Agent']);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            $error = 'cURL Error: ' . curl_error($ch);
            curl_close($ch);
            throw new Exception($error);
        }

        curl_close($ch);
        return $response;
    }

    /**
     * 解析API返回的JSON响应。
     *
     */
    private function parseResponse($response) {
        $data = json_decode($response, true);

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new Exception('JSON Decode Error: ' . json_last_error_msg());
        }

        if (!isset($data['boxOffice']['data']['list']) || !is_array($data['boxOffice']['data']['list'])) {
            throw new Exception('Invalid API response structure.');
        }
        return $data['boxOffice']['data']['list'];
    }

    /**
     * 从API返回的电影列表中提取所需的电影信息。
     *
     */
    private function extractMovieInfo($apiMovieList) {
        $movies = [];
        foreach ($apiMovieList as $movie) {
            if (isset($movie['movieInfo']['movieName']) && isset($movie['movieInfo']['releaseInfo']) && isset($movie['boxDesc']) && isset($movie['sumBoxDesc'])) {
                $movies[] = [
                    'movieName' => $movie['movieInfo']['movieName'],
                    'releaseInfo' => $movie['movieInfo']['releaseInfo'],
                    'boxDesc' => $movie['boxDesc'],
                    'sumBoxDesc' => $movie['sumBoxDesc']
                ];
            }
        }
        return $movies;
    }

    /**
     * 获取电影票房信息。
     * 这是一个公共方法，用于获取、解析和提取电影数据。
     */
    public function getMovies() {
        try {
            $response = $this->fetchData();
            $apiMovieList = $this->parseResponse($response);
            return $this->extractMovieInfo($apiMovieList);
        } catch (Exception $e) {
            return ['error' => $e->getMessage()];
        }
    }
}

// 使用API类
$api = new MaoyanAPI();
$moviesData = $api->getMovies();
$moviesData['developer'] = 'lzy';

echo json_encode($moviesData, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

?>